package com.jiukuaidao.zhulin.module.user;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jiukuaidao.library.base.BaseActivity;
import com.jiukuaidao.library.util.ToastUtil;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.data.TaskProvider;
import com.jiukuaidao.zhulin.module.CommContract;
import com.jiukuaidao.zhulin.module.CommPresenter;
import com.jiukuaidao.zhulin.module.CommWatcher;
import com.jiukuaidao.zhulin.module.user.contract.LoginQuickContract;
import com.jiukuaidao.zhulin.module.user.presenter.LoginQuickPresenter;
import com.jiukuaidao.zhulin.util.UserInfoUtil;

import butterknife.BindView;

/**
 * Author : PaiTaSuo
 * Date : 2016/5/25
 * Description : 手机号快捷登录
 */
public class LoginQuickActivity extends BaseActivity<LoginQuickContract.Presenter> implements View.OnClickListener, LoginQuickContract.View, CommContract.CodeView {
    @BindView(R.id.titile_left_imageview)
    ImageView mLeftImg;
    @BindView(R.id.titile_text)
    TextView mTitileText;
    @BindView(R.id.titile_right_text)
    TextView mTitileRightText;
    @BindView(R.id.et_login_username)
    EditText mEtUname;
    @BindView(R.id.et_login_password)
    EditText mEtPsw;
    @BindView(R.id.bt_code_login)
    TextView mBtCodeLogin;
    @BindView(R.id.bt_login)
    Button mBtLogin;
    @BindView(R.id.tv_phone_login)
    TextView mTvPhoneLogin;

    private CommPresenter mCommPresenter;
    public static LoginQuickActivity mInstance;

    @Override
    protected View getTargetView() {
        return null;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_quick_login;
    }

    @Override
    protected void handleBundle(Bundle bundle) {

    }

    @Override
    protected void init() {
        mInstance = this;
        mTitileText.setText(R.string.user_fast_login_tv);
        mTitileRightText.setText(R.string.phone_register);
//        mEtUname.addTextChangedListener(new CommWatcher(mBtCodeLogin, CommWatcher.CODEBTN));
        mEtPsw.addTextChangedListener(new CommWatcher(mBtLogin, CommWatcher.LOGINBTN));
        mCommPresenter = new CommPresenter(TaskProvider.provideTasksRepository(this), this);
        new LoginQuickPresenter(TaskProvider.provideTasksRepository(this), this);
        setOnClickListener();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_login:
                mPresenter.start();
                break;
            case R.id.bt_code_login:
                mCommPresenter.getVeriCode();
                break;
            case R.id.tv_phone_login:
                finish();
                break;
            case R.id.titile_right_text:
                intentJump(RegisterActivity.class);
                break;
            case R.id.titile_left_imageview:
                finish();
                break;
        }

    }

    public void setOnClickListener() {
        mBtCodeLogin.setOnClickListener(this);
        mBtLogin.setOnClickListener(this);
        mTvPhoneLogin.setOnClickListener(this);
        mTitileRightText.setOnClickListener(this);
        mLeftImg.setOnClickListener(this);
    }


    @Override
    public String getCodeMobile() {
        return mEtUname.getText().toString();
    }

    @Override
    public TextView codeBtn() {
        return mBtCodeLogin;
    }

    @Override
    public void codeToast(String msg, String errorCode) {
        ToastUtil.show(msg, Toast.LENGTH_SHORT);
        if (errorCode.equals("1010")) {
            GraphicCodeDialog codeDialog = new GraphicCodeDialog(LoginQuickActivity.this);
            codeDialog.showDialog();
        }
    }

    @Override
    public String getPhoneNum() {
        return mEtUname.getText().toString();
    }

    @Override
    public String getCodeStr() {
        return mEtPsw.getText().toString();
    }

    @Override
    public String getUserIp() {
        return UserInfoUtil.getUserIp();
    }

    @Override
    public String getAppKey() {
        return UserInfoUtil.get_app_key(this);
    }

    @Override
    public void jumpActivity() {
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void toast(String msg) {
        ToastUtil.show(msg, Toast.LENGTH_SHORT);
    }
}
